package cn.wolfcode.cms.web.controller;

import cn.wolfcode.cms.annotation.PermissionName;
import cn.wolfcode.cms.domain.Depot;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.IDepotService;
import cn.wolfcode.cms.util.JSONResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//仓库
@Controller
@RequestMapping("depot")
public class DepotController {

    @Autowired
    private IDepotService depotService;

    @RequiresPermissions("depot:index")
    @PermissionName("仓库首页")
    @RequestMapping("")
    public String index() {
        depotService.list();
        return "depot/list";
    }

    //仓库信息列表/带分页
    @RequiresPermissions("depot:list")
    @PermissionName("仓库首页")
    @RequestMapping("list")
    @ResponseBody
    public Object list(QueryObject qo) {
        return depotService.query(qo);
    }


    //所有仓库信息数据/无分页
    @RequestMapping("listData")
    @ResponseBody
    public Object listData() {
        return depotService.list();
    }


    //仓库添加
    @RequiresPermissions("depot:add")
    @PermissionName("仓库添加")
    @RequestMapping("save")
    @ResponseBody
    public Object save(Depot entity) {
        JSONResult result = new JSONResult();
        try {
            depotService.save(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("添加失败");
        }
        return result;
    }

    //仓库编辑
    @RequiresPermissions("depot:edit")
    @PermissionName("仓库编辑")
    @RequestMapping("edit")
    @ResponseBody
    public Object edit(Depot entity) {
        JSONResult result = new JSONResult();
        try {
            depotService.update(entity);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("编辑失败");
        }
        return result;
    }

    //仓库删除
    @RequiresPermissions("depot:delete")
    @PermissionName("仓库删除")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id) {
        JSONResult result = new JSONResult();
        try {
            depotService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }
}
